#include "enemy_4.h"

extern int G_W,G_H;

ENEMY_4::ENEMY_4(SCENE *scene, QTimeLine *global_tl, CASH *cash):QGraphicsObject()
{

    go = new SOUND_POWER(cash,"Res/Sound/Enemy/Enemy_4/go.ogg",this);
    go->Play_Now();

    counter_for_fire=0;

    stvol_1_go_to_left=QPoint(257,24);
    stvol_2_go_to_left=QPoint(257,99);

    stvol_1_go_to_right=QPoint(52,25);
    stvol_2_go_to_right=QPoint(52,99);

    qsrand(QTime::currentTime().second());

    if(0+qrand()%(1-0+1)){
        im = cash->cash_pixmap->value("Res/Ships/Enemy/Enemy_4/Enemy_4_go_to_left.png");
        respaun_point= QPoint(G_W+im->width(), (0+qrand()%(G_H/2-0+1)) );
        go_to_left=true;
    }else{
        im = cash->cash_pixmap->value("Res/Ships/Enemy/Enemy_4/Enemy_4_go_to_right.png");
        respaun_point= QPoint( (im->width())*(-1) , (0+qrand()%(G_H/2-0+1)) );
        go_to_left=false;
    }

    point=15;

    if(go_to_left)
        border = cash->cash_poly->value("Res/Ships/Enemy/Enemy_4/Enemy_4_go_to_left.poly");
    else
        border = cash->cash_poly->value("Res/Ships/Enemy/Enemy_4/Enemy_4_go_to_right.poly");

    this->setZValue(0.5);
    this->setPos(respaun_point);     // Начальная позиция

    anim_enemy = new QGraphicsItemAnimation();

    enemy_time_line = new QTimeLine(1000);                                             // Скорость перемещения обьекта по траектории
    enemy_time_line->setEasingCurve( QEasingCurve::Type(1));

    enemy_time_line->setFrameRange(0, 1);
    anim_enemy->setTimeLine(enemy_time_line);
    anim_enemy->setItem(this);

    anim_enemy->setPosAt(0,respaun_point);

    if(go_to_left)
        anim_enemy->setPosAt(1,QPointF( (G_W+im->width())*(-1),respaun_point.y()) );
    else
        anim_enemy->setPosAt(1,QPointF( G_W+im->width()+im->width(),respaun_point.y()) );

    fire_now_1=2+qrand()%(8-2+1);
    fire_now_2=2+qrand()%(8-2+1);
    fire_now_3=2+qrand()%(8-2+1);

    timer_id=startTimer(100);

    enemy_time_line->start();

    collisia_control = new COLLISIA_CONTROL(scene,((QGraphicsObject*)this),this,global_tl,cash,10);

    connect(collisia_control,SIGNAL(Collisia(QPointF,int)),this,SLOT(Damage_Detected(QPointF,int)));
    connect(enemy_time_line,SIGNAL(finished()),SLOT(deleteLater()));

}

QPainterPath ENEMY_4::shape() const
{

    return *border;

}

QRectF ENEMY_4::boundingRect() const
{

    return QRectF( QPointF(0,0),im->size() );

}

void ENEMY_4::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{

    painter->save();
    painter->drawPixmap( 0,0,*im );
    painter->restore();

}

void ENEMY_4::timerEvent(QTimerEvent *)
{

    ++counter_for_fire;

    if(counter_for_fire==fire_now_1 || counter_for_fire==fire_now_2 || counter_for_fire==fire_now_3){
        if(go_to_left){
            emit Fire( QPoint(this->pos().x()+stvol_1_go_to_left.x(),this->pos().y()+stvol_1_go_to_left.y()) );
            emit Fire( QPoint(this->pos().x()+stvol_2_go_to_left.x(),this->pos().y()+stvol_2_go_to_left.y()) );
        }
        else{
            emit Fire( QPoint(this->pos().x()+stvol_1_go_to_right.x(),this->pos().y()+stvol_1_go_to_right.y()) );
            emit Fire( QPoint(this->pos().x()+stvol_2_go_to_right.x(),this->pos().y()+stvol_2_go_to_right.y()) );
        }
    }

}

ENEMY_4::~ENEMY_4()
{
}

int ENEMY_4::type() const
{

    return point;

}

void ENEMY_4::Damage_Detected(QPointF a, int d)
{

    a = QPointF();
    point-=d;

    if(point<=0)
        if(this->scene()){
            disconnect(enemy_time_line,SIGNAL(finished()),this,SLOT(deleteLater()));
            killTimer(timer_id);
            go->Stop();
            emit Kill_Me_Please((QGraphicsObject*)this);
        }


}
